//
//  HouseCleanDetailViewController.m
//  Housekeeping
//
//  Created by 辰领科技 on 16/5/18.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "HouseCleanDetailViewController.h"
#import "HouseCleanDtailCollectionViewCell.h"
#import "DetailServiceTableViewCell.h"
#import "SureIndentViewController.h"
static NSString * CellIndentfier = @"DetailServiceTableViewCell";
static NSString * cellIndentifer = @"HouseCleanDtailCollectionViewCell";
@interface HouseCleanDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *serViceRang;

@property(nonatomic,strong)NSArray * services;
@property(nonatomic,strong)NSArray * detailServices;
@end

@implementation HouseCleanDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _services = [NSArray array];
    _detailServices = [NSArray array];
    [self initViewLayout];
    [self getServiceDetail];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 675), NO);
    
    UICollectionViewFlowLayout *FlowLayout =[[UICollectionViewFlowLayout alloc]init];
    FlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    FlowLayout.minimumInteritemSpacing = 0*VerticalRatio();
    FlowLayout.minimumLineSpacing = 0*VerticalRatio();
    self.collectionView.collectionViewLayout = FlowLayout;
    self.collectionView.userInteractionEnabled=YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [self.collectionView registerNib:[UINib nibWithNibName:cellIndentifer bundle:nil] forCellWithReuseIdentifier:cellIndentifer];

 [self.tableView registerNib:[UINib nibWithNibName:CellIndentfier bundle:nil] forCellReuseIdentifier:CellIndentfier];

}


- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark ==== 请求图文详情
- (void)getServiceDetail{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_serviceId forKey:@"goodsId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryAppMallGoodsServiceRange withHttpType:@"POST" withTag:HK_HK_queryAppMallGoodsServiceRange];
}

#pragma mark == 立即预约
- (IBAction)buyNow:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"IndentsStoryboard" bundle:nil];
    SureIndentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SureIndent"];
    if ([Common dataIsNULL:_houseSize]) {
        [CWHUDTips showTips:@"请输入房间大小"];
        [self backBtn];
        return;
    }
    if ([Common dataIsNULL:_dateTime]) {
        [CWHUDTips showTips:@"请选择预约时间"];
        [self backBtn];
        return;
    }

    page.productDic = [_productDic mutableCopy];
    page.countSize = _houseSize;
    page.dateTime = _dateTime;
    [[Common getNavigation]pushViewController:page];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50*VerticalRatio();
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _detailServices.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    DetailServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentfier];
    if (cell == nil) {
        cell= [[DetailServiceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentfier];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    NSDictionary * dic = _detailServices[indexPath.row];
    NSString * image = [NSString stringWithFormat:@"%@",dic[@"mgssImage"]];
    if (image && image.length>0) {
        [cell.detailServiceImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]forState:UIControlStateNormal];
    }

    cell.detailServiceName.text = [NSString stringWithFormat:@"%@",dic[@"mgssName"]];
   
    
    return cell;
    
    
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _services.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(106*VerticalRatio(), 125*VerticalRatio());;
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0,0, 0*VerticalRatio(), 0*VerticalRatio());
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
           HouseCleanDtailCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifer forIndexPath:indexPath];
        NSDictionary * dic = _services[indexPath.row];
        NSString * image = [NSString stringWithFormat:@"%@",dic[@"mgsrImage"]];
        if (image && image.length>0) {
            [cell.serviceImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        }
        cell.serviceName.text = [NSString stringWithFormat:@"%@",dic[@"msgrName"]];
        return cell;
  
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==HK_HK_queryAppMallGoodsServiceRange) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _serViceRang.text = [NSString stringWithFormat:@"注意： %@",database[@"result"][@"mgooServiceAttention"]];
            _services = database[@"result"][@"listGoodsServiceRange"];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            [_collectionView reloadData];
            _detailServices = database[@"result"][@"listGoodsServiceStandard"];
            _tableView.dataSource = self;
            _tableView.delegate = self;
            [_tableView reloadData];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
